<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>后台管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="__STATIC__/res/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="__STATIC__/res/adminui/dist/css/admin.css" media="all">
    <style>
        .form-submit{
            margin:0px 15px 0 15px;
            padding-top: 15px;
            padding-left: 15px;
            position: fixed;
            bottom: 0;
            left: 0;
            z-index: 5;
            background: #fff;
            width: 100%;
        }
        .layui-upload-img{
            with: 50px;
        }
    </style>
</head>
<body style="margin-bottom: 50px;">

    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">

                    <div class="layui-card-body" pad15>
                        <form class="layui-form" lay-filter="">
                            <div class="layui-tab layui-tab-brief">

                                <ul class="layui-tab-title">
                                    {foreach $websiteConfigInfo as  $k=>$group}
                                    <li {if $k eq 'base'}class="layui-this" {/if}>{$group.config_group_name}</li>
                                    {/foreach}
                                </ul>

                                <div class="layui-tab-content">
                                    {foreach $websiteConfigInfo as  $k=>$group}
                                    <div class="layui-tab-item {if $k eq 'base'}layui-show{/if}">
                                        {foreach $group.website_configs as $config}
                                        <div class="layui-form-item">
                                            <label class="layui-form-label">{$config.name} </label>
                                            <div class="layui-input-inline" style="width: 70%">
                                                {if $config.field_type == "input" || $config.field_type eq "datetime" ||
                                                $config.field_type eq "date"}
                                                <input type="text" name="{$config.config_code}" id="{$config.config_code}"
                                                       placeholder="请输入配置信息" autocomplete="off"
                                                       class="layui-input" value="{$config.config_value}">
                                                {/if}

                                                {if $config.field_type == "textarea"}
                                                <textarea type="text" name="{$config.config_code}" lay-verify="required"
                                                          placeholder="请输入配置信息" autocomplete="off"
                                                          style="height: 150px;"
                                                          class="layui-textarea ">{$config.config_value}</textarea>
                                                {/if}

                                                {if $config.field_type eq "image"}
                                                <button type="button" class="layui-btn" id="upload-{$config.config_code}" >
                                                    <i class="layui-icon">&#xe67c;</i>上传图片
                                                </button>

                                                <input type="hidden" name="{$config.config_code}" id="{$config.config_code}-url" value="{$config.config_value}"/>
                                                {/if}

                                            </div>
                                            <div class="layui-form-mid layui-word-aux"> $config.{$config.config_code}</div>
                                        </div>
                                        {if $config.field_type eq "image"}
                                        <div class="layui-form-item {$config.config_code}-url">
                                            <label class="layui-form-label">
                                                {if $config.fullConfigValue neq ''}
                                                <a href="javascript:;" title="点击删除">
                                                    <i class="layui-icon layui-icon-delete" style="font-size:30px;color: #1E9FFF;"></i>
                                                </a>
                                                {/if}
                                            </label>

                                            <div id="{$config.config_code}-show">
                                                {if $config.fullConfigValue neq ''}
                                                    <img src="{$config.fullConfigValue}" width="200">
                                                {/if}
                                            </div>
                                        </div>
                                        {/if}
                                        {/foreach}
                                    </div>
                                    {/foreach}
                                </div>
                            </div>

                            <div class="form-submit">
                                <div class="layui-form-item">
                                    <div class="layui-input-black">
                                        <input type="button" lay-submit lay-filter="layuiadmin-app-form-edit"
                                               id="layuiadmin-app-form-edit"
                                               value="保存" class="layui-btn">
                                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                    </div>
                                </div>
                            </div>

                        </form>
                    </div>

                </div>
            </div>
        </div>
    </div>


<script>
    var WEBSITE_CONFIG_DO_SETTING_URL = '{:url("websiteConfig/doSetting",["website_code"=>$websiteCode])}'
    var WEBSITE_CONFIG_UPLOAD_IMAGE_URL ='{:url("websiteConfig/upload",["website_code"=>$websiteCode])}'
</script>
<script src="__STATIC__/res/layui/layui.js"></script>
<script>
    layui.config({
        base: '__STATIC__/res/' //静态资源所在路径
    }).use(['element', 'index', 'form', 'laydate', 'upload','element'], function () {
        var $ = layui.$
            , form = layui.form
            ,laydate = layui.laydate
            ,element = layui.element
            ,upload = layui.upload;
        function uploadImage(fieldName,uploadUrl){
            var uploadInst = upload.render({
                elem: '#upload-'+fieldName
                ,url: uploadUrl
                , before: function(){
                    layer.load();
                }
                ,done: function(res){
                    if(res.result.status){
                        layer.msg(res.result.msg, {
                            offset: '15px'
                            , icon: 2
                            , time: 1000
                        });
                        layer.closeAll('loading');
                        return ;
                    }

                    //上传完毕回调
                    $('#'+fieldName+'-show').html("<img src='" + res.result.data.fullPath + "' style='width:200px'>")
                    $('#'+fieldName+'-url').val(res.result.data.path)
                    $('.'+fieldName+'-url i').show()
                    layer.closeAll('loading');
                }
                ,error: function(){
                    //请求异常回调
                    layer.closeAll('loading');
                }
            });

            $('.'+fieldName+'-url').on('click','i',function(){
                $(this).hide()
                $(this).parents('.layui-form-item').find('#'+fieldName+'-show img').remove();
                $(this).parents('.layui-form-item').prev('.layui-form-item').find('#'+fieldName+'-url').val('')
            })
        }

        {foreach $websiteConfigInfo as $group}
            {foreach $group.website_configs as $config}

                {if $config.field_type eq 'date'}
                    laydate.render({
                        elem: '#{$config.config_code}'
                        , trigger: 'click'
                        , type: 'date'
                        , format: 'yyyy-MM-dd'
                        , position: 'fixed'
                        , closeStop: '#{$config.config_code}'
                    });
                {/if}

                {if $config.field_type eq 'datetime'}
                laydate.render({
                    elem: '#{$config.config_code}'
                    , trigger: 'click'
                    , type: 'datetime'
                    , format: 'yyyy-MM-dd HH:mm:ss'
                    , position: 'fixed'
                    , closeStop: '#{$config.config_code}'
                });
                {/if}

                {if $config.field_type eq "image"}
                  uploadImage('{$config.config_code}',WEBSITE_CONFIG_UPLOAD_IMAGE_URL)
                {/if}

            {/foreach}
        {/foreach}

        //监听提交
        form.on('submit(layuiadmin-app-form-edit)', function (data) {
            var field = data.field; //获取提交的字段
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            // console.log(field)

            //提交 Ajax 成功后，关闭当前弹层并重载表格
            $.ajax({
                method: 'post',
                data: field,
                url: WEBSITE_CONFIG_DO_SETTING_URL,
                success: function (res) {
                    if (res.result.status) {
                        layer.msg(res.result.msg, {
                            offset: '15px'
                            , icon: 2
                            , time: 1000
                        });
                        return
                    }
                    layer.msg(res.result.msg, {
                        offset: '15px'
                        , icon: 1
                        , time: 1000
                    },function (){
                        window.location.reload(true)
                    });
                }
            });
        });

    })

</script>
</body>
</html>